<?php

namespace App\Admin\Controllers;

use App\Admin\Models\SequenceModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets;
use Encore\Admin\Layout\Content;
use App\Admin\Forms\Employee;
use App\Admin\Models\EmployeeModel;

class EmployeeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Member List';

    /**
     * Set description for following 4 action pages.
     *
     * @var array
     */
    protected $description = [
        'index'  => '这里展示的是人员列表，绑定后台管理员账号后，教师即可登录后台维护自己的个人信息',
        'show'   => '人员列表详情',
        'edit'   => '请在此处编辑人员列表，绑定后台管理员账号后，教师即可登录后台维护自己的个人信息',
        'create' => '请创建新的人员列表，绑定后台管理员账号后，教师即可登录后台维护自己的个人信息',
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new EmployeeModel);

        $sequences = SequenceModel::getSequenceList(['parent_id' => 0]);
        !empty($sequences) && $sequences = array_column($sequences, 'name_en', 'sequence_id');

        // 添加筛选条件
        $grid->model()->orderBy('order', 'asc');
        $grid->model()->orderBy('employee_id', 'desc');

//        $grid->column('employee_id', __('ID'))->width(60)->sortable();
        $grid->column('order', __('优先级'))->width(100)->sortable();
        $grid->column('avatar', __('个人照片'))->display(function ($imageUrl) {
            return '<img src="' . (!empty($imageUrl) ? adminUrl($imageUrl) : defaultUserImage()) . '" style="width:150px;" class="img img-thumbnail">';
        })->width(160);
        $grid->column('name_zh', __('姓名-中文'));
        $grid->column('name_en', __('姓名-英文'));
        $grid->column('sequence_pid', __('岗位分组'))->display(function ($sequence_pid) use ($sequences) {
            // $sequence_pid 是个数组，有多个则需要展示多个名称
            $sequence_name = [];
            if (!empty($sequence_pid)) {
                foreach ($sequence_pid as $sequence_id) {
                    if (empty($sequences[$sequence_id])) {
                        continue;
                    }
                    $sequence_name[] = $sequences[$sequence_id];
                }
            }
            return implode('、', $sequence_name);
        });
        $grid->column('email', __('邮箱地址'));
        $grid->column('created_at', __('创建时间'))->sortable()->hide();
        $grid->column('updated_at', __('更新时间'))->sortable()->hide();

        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name_zh', __('姓名-中文'));
            $filter->like('name_en', __('姓名-英文'));

        });

        // 禁用导出数据按钮
        $grid->disableExport();

        // 设置样式
        \Encore\Admin\Admin::style('.column-__row_selector__ {width: 70px;} .column-__actions__ {width: 70px;}');

        return $grid;
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['show'] ?? trans('admin.show'))
            ->body(Widgets\Tab::forms([
                'basic'    => Employee\Basic::class,
                'optional' => Employee\Optional::class,
                'custom'   => Employee\Custom::class,
            ]));
    }

    /**
     * 编辑页面
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body(Widgets\Tab::forms([
                'basic'    => Employee\Basic::class,
                'optional' => Employee\Optional::class,
                'custom'   => Employee\Custom::class,
            ]));
    }


    /**
     * Create interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body(Widgets\Tab::forms([
                'basic'    => Employee\Basic::class,
                'optional' => Employee\Optional::class,
                'custom'   => Employee\Custom::class,
            ]));
    }
}
